/**
 * 
 */
package com.cuc.platform.core.web.sysadmin.security;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuc.platform.core.componet.security.domain.SecurityMenu;
import com.cuc.platform.core.componet.security.service.SecurityMenuService;

/**
 * @author peepul
 *
 * 菜单维护控制器
 */
@Controller
@RequestMapping(value = "/security/securitymenu")  
public class SecurityMenuController {

	/**
	 * 列出菜单列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@RequestParam String loginName, Model model){
		List<SecurityMenu> list = securityMenuService.getAllMenus();
		model.addAttribute("list", list);
		
		return "sysadmin/security/menu/menuList";
	}
	
	/**
	 * 根据id查看指定的信息
	 */
	@RequestMapping(params = "method=viewInfo")
	public String viewInfo(@RequestParam Long id, Model model){
		SecurityMenu menu = securityMenuService.getMenuById(id);
		model.addAttribute("menu", menu);
		
		return "sysadmin/security/menu/menuView";
	}
	
	/**
	 * 根据id查看要新增的信息
	 */
	@RequestMapping(params = "method=viewAdd")
	public String viewAdd(){
		return "sysadmin/security/menu/menuEdit";
	}	  
	 
	/**
	 * 根据id查看要编辑的信息
	 */
	@RequestMapping(params = "method=viewEdit")
	public String viewEdit(@RequestParam Long id, Model model){
		SecurityMenu menu = securityMenuService.getMenuById(id);
		model.addAttribute("menu", menu);
		
		return "sysadmin/security/menu/menuEdit";
	}	
	
	/**
	 * 保存
	 */
	@RequestMapping(params = "method=save", method=RequestMethod.POST)
	public String save(SecurityMenu menu){
		
		//如果id为空，则是新增，否则为修改
		Long id = menu.getId();
		if (null != id) {
			securityMenuService.updateMenu(menu);//修改
		} else{
			securityMenuService.insertMenu(menu);//新增
		}
		
		return "redirect:securitymenu.do?method=list";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(params = "method=delete")
	public String delete(@RequestParam Long id){
		securityMenuService.delMenu(id);
		
		return "redirect:securitymenu.do?method=list";
	}
	
	@RequestMapping(params = "method=deleteOPPSingle")
	public String deleteOPPSingle(@RequestParam String id){
		String[] ids=id.split(",");
		for(int i=0;i<ids.length;i++)
		{
			String  idop =ids[i];
			Long idoper=Long.parseLong(idop);
			securityMenuService.delMenu(idoper);
		}
		
		return "redirect:securitymenu.do?method=list";
	}
	@RequestMapping(params = "method=viewSelectRoles")
	public String viewSelectRoles(@RequestParam Long id, String name, Model model){
		List<SecurityMenu> list = securityMenuService.viewRoleswithMenus(id);
		model.addAttribute("list", list);
		model.addAttribute("id", id);
		model.addAttribute("name", name);
		
		return "sysadmin/security/menu/selectRoles";
	}
	
	@RequestMapping(params = "method=saveRoles", method=RequestMethod.POST)
	public String saveRoles(HttpServletRequest request){
		
		String menu_id = (String)request.getParameter("id");
		String[] role_ids = (String[])request.getParameterValues("roleArray");

		securityMenuService.saveMenuRoleUnions(menu_id, role_ids);
		
		return "redirect:securitymenu.do?method=list";
	}
	
	@RequestMapping(params = "method=listLevelByUser")
	@ResponseBody
	public List<SecurityMenu> listLevelByUser(@RequestParam String username, @RequestParam Integer level){
		List<SecurityMenu> list = securityMenuService.listLevelByUser(username, level);
		
		return list;
	}
	
	/**
	 * Ioc
	 */
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(SecurityMenuController.class);
	
	@Resource
	private SecurityMenuService securityMenuService;
}
